refactor: add Chunk type parameter to Stream class for type-safe streaming#42
Conversation
…aming Make Stream generic over Chunk type to enable capability-specific chunk types, improving type safety and IDE support across all streaming implementations. Changes: - Add Chunk type parameter to Stream[Out, Params, Chunk] base class - Update all provider streaming implementations to use specific chunk types (TextGenerationChunk, ImageGenerationChunk) instead of generic Chunk - Update Client.stream() and _stream_class() return types to include Chunk - Remove generic Chunk imports from provider implementations - Update all tests to use new Stream signature - Remove streaming modules from mypy ignore lists (now properly typed) - Use ApplicationMimeType.JSON constant in HTTP tests Benefits: - Type-safe chunk parsing per capability - Better IDE autocomplete and type checking - Cleaner provider code without generic Chunk imports - Improved mypy compliance
Code Review: Chunk Type Parameter RefactoringThank you for this PR! This is a well-executed refactoring that significantly improves type safety across the streaming implementations. Here's my detailed review: ✅ Strengths
🔍 Code Quality ObservationsMinor: Import Organization (src/celeste/streaming.py:9-10) from celeste.io import Chunk as ChunkBase
from celeste.io import OutputConsider whether this alias is necessary or if the base class could have a distinct name (though this may be out of scope for this PR). Good: Variance Annotation (src/celeste/streaming.py:14) Observation: Client Return Types (src/celeste/client.py:78, 163)
However, the
This is acceptable but worth noting for future maintainability. 🎯 Best Practices✅ Single Responsibility: Each change serves the single purpose of improving type safety 🔒 Security Considerations✅ No security concerns identified ⚡ Performance Considerations✅ No performance impact - this is a compile-time type system change 🐛 Potential IssuesNone identified. The implementation appears solid. 📝 Suggestions
🎉 ConclusionThis is a high-quality refactoring that achieves its stated goals:
The changes are well-structured, consistently applied, and properly tested. The removal of mypy ignores for streaming modules is particularly notable as evidence of success. Recommendation: Approve and merge ✅ Great work on improving the type safety of the codebase! This will make the streaming API much more pleasant to work with. |
Summary
This PR refactors the Stream class to be generic over a Chunk type parameter, enabling capability-specific chunk types and improving type safety across all streaming implementations.
Changes
Stream[Out, Params, Chunk]base classTextGenerationChunkImageGenerationChunkClient.stream()and_stream_class()return types to include Chunk parameterChunkimports from provider implementationsApplicationMimeType.JSONconstant in HTTP testsBenefits
Testing
All pre-commit hooks passed: